<template>
  <div class="like">
    <!-- 导航栏 -->
    <van-nav-bar title="喜欢的歌">
      <template #left>
        <van-icon name="arrow-left" size="20" @click="goBack" />
      </template>

      <template #right>
        <van-icon name="search" size="20" @click="goSearch" />
      </template>
    </van-nav-bar>

    <!-- 歌曲列表 -->
    <div
      class="tabbox"
      v-for="(item, index) in Likesong"
      :key="index"
      @click="
        playAudio(item.id, item.img, item.name, item.singname, item.singid)
      "
    >
      <!-- 封图 -->
      <div class="sealing">
        <van-image :src="item.img" lazy-load>
          <template v-slot:loading>
            <van-loading type="spinner" size="20" />
          </template>
        </van-image>
      </div>

      <!-- 名字 -->
      <div class="tabname">
        <div class="tabsongname">{{ item.name }}</div>
        <div class="tabtxt">
          <div class="textbox">SQ</div>
          {{ item.singname }}
        </div>
      </div>

      <div class="play">
        <van-icon
          :name="
            audioId !== item.id
              ? 'play-circle-o'
              : isPaused
              ? 'play-circle-o'
              : 'pause-circle-o'
          "
        />
      </div>

      <div class="share" @click="cancelLikesong(index)">
        <van-icon name="like" size="20" color="red" />
      </div>
    </div>

    <!-- 播放器 -->
    <Playbox></Playbox>
  </div>
</template>

<script>
import Playbox from "../components/Playbox.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Like",

  components: {
    Playbox,
  },

  data() {
    return {
      uid: "",
    };
  },

  computed: {
    ...mapState(["Likesong", "audioId", "isPaused", "audioEle"]),
  },

  created() {
    //判断是否登录
    this.uid = this.$cookies.get("uid");

    //获取本地喜欢歌曲数据
    let Likesongs = localStorage.getItem("Likesong");
    if (Likesongs) {
      Likesongs = JSON.parse(Likesongs);
      this.changeStateProperty({
        key: "Likesong",
        value: Likesongs,
      });
    }
  },

  methods: {
    //返回上一级
    goBack() {
      this.$router.go(-1);
    },

    //跳转到搜索页面
    goSearch() {
      this.$router.push({ name: "Search" });
    },

    //取消喜欢的歌曲
    cancelLikesong(index) {
      this.Likesong.splice(index, 1);
      localStorage.setItem("Likesong", JSON.stringify(this.Likesong));
    },

    ...mapMutations(["changeStateProperty"]),

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName, singerId) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
      this.changeStateProperty({ key: "singerId", value: singerId });

      //获取专辑评论数据
      this.axios({
        method: "get",
        url: "/comment/music",
        params: {
          id,
        },
      })
        .then((res) => {
          this.changeStateProperty({
            key: "audioComment",
            value: res.data.hotComments,
          });
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
// 导航栏
/deep/.van-nav-bar .van-icon {
  color: #000000;
}

/deep/.van-nav-bar__title {
  font-weight: 600;
}

//歌曲列表
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding: 0 14px;
  margin-top: 10px;
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabtxt {
  display: flex;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.play {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 40px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: auto;
}
</style>